import openpyxl
import pandas as pd

import TimeUtil
import util
from 单用户中断时长工作.Input import get_four_path, get_statistic_path


def w1(d):
    start_row = 231
    start_row_2 = 232

    df = pd.read_excel(get_four_path(date))
    dfm = pd.read_csv(get_statistic_path(date))

    workbook = openpyxl.load_workbook(f"D:\\中断输入\\输出\\{month}.{d - 1}日单用户中断时长各分公司指标.xlsx")
    sheet = workbook['单用户中断时长']
    # 修改新插入行的单元格内容
    for i in range(start_row, start_row + 16):
        sheet.cell(row=i, column=d + 2).value = df['单用户中断时长'].loc[i - start_row]

    sheet = workbook['中断用户平均修复时长']
    # 修改新插入行的单元格内容
    for i in range(start_row, start_row + 16):
        sheet.cell(row=i, column=d + 4).value = df['中断用户平均修复时长'].loc[i - start_row]
        sheet.cell(row=i, column=3).value = dfm['中断时长总和'].loc[i - start_row]
        sheet.cell(row=i, column=4).value = dfm['中断用户数'].loc[i - start_row]

    sheet = workbook['用户平均中断率']
    # 修改新插入行的单元格内容
    for i in range(start_row_2, start_row_2 + 16):
        sheet.cell(row=i, column=d + 2).value = df['用户平均中断率'].loc[i - start_row_2]

    sheet = workbook['用户平均中断次数']
    # 修改新插入行的单元格内容
    for i in range(start_row_2, start_row_2 + 16):
        sheet.cell(row=i, column=d + 4).value = df['用户平均中断次数'].loc[i - start_row_2]
        sheet.cell(row=i, column=3).value = dfm['中断总次数'].loc[i - start_row_2]
        sheet.cell(row=i, column=4).value = dfm['中断用户数'].loc[i - start_row_2]

    workbook.save(f"D:\\中断输入\\输出\\{month}.{d}日单用户中断时长各分公司指标.xlsx")


def w2(d):
    df = pd.read_excel(get_four_path(date))
    df['用户平均中断率'] = df['用户平均中断率'] / 100

    dfm = pd.read_csv(get_statistic_path(date))
    workbook = openpyxl.load_workbook(f"D:\\中断输入\\输出\\{month}月指标.xlsx")
    target_sheet = workbook.copy_worksheet(workbook[f'{month}.{d - 1}'])
    target_sheet.title = f'{month}.{d}'
    target_sheet.cell(row=2, column=16).value = d
    target_sheet.cell(row=21, column=1).value = f'单日中断明细指标-{d}号-v4'

    for col in range(2, 11):
        for row in range(3, 19):
            target_sheet.cell(row=row, column=col).value = df.iloc[row - 3, col - 1]

    for r in range(3, 19):
        target_sheet.cell(row=r, column=14).value = dfm['fttr当月'].loc[r - 3]
        target_sheet.cell(row=r, column=15).value = dfm['fttr当日'].loc[r - 3]

    workbook.save(f"D:\\中断输入\\输出\\{month}月指标.xlsx")


"""
每月初需要修改模版，以及注意前一天的文件名
月初
1.新建"D:\\中断输入\\输出\\2.0日单用户中断时长各分公司指标.xlsx"
2.月指标新增2.0sheet
"""
if __name__ == '__main__':
    month = '3'
    a = 29
    b = a + 1
    for i in range(a, b):
        date = TimeUtil.parse(f'2025-03-{util.get_day(i)}')
        day = i
        w1(day)
        w2(day)
